package com.yssoft.controller.core;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.yssoft.entity.core.TIndustryCategory;
import com.yssoft.service.core.IndustryCategoryService;

/**
 * @author 王永宁
 * @time 2018/4/25 下午 15:30
 * @  行业类别控制器
 */
@Controller
@RequestMapping("/industryCategory")
public class IndustryCategoryController {

		@Resource
	    private IndustryCategoryService industryCategoryService;
	    
		 /**
	     * 跳转到行业类别页面
	     * @return
	     */
	    @RequestMapping("/industryCategoryPage")
	    public String findIndustryCategoryPage(Model model){
	        model.addAttribute("json",JSONArray.toJSON(industryCategoryService.queryIndustryTableTree()));
	        return "core/industryCategory/industryCategoryList1";
	    }

	    
	    /**
	     * 获取行业类别数据
	     * @param page
	     * @param limit
	     * @return
	     */
	    @RequestMapping("/findIndustryCategoryPage")
	    @ResponseBody
	    public String findIndustryPage(@RequestParam(name = "page",defaultValue = "0") int page , @RequestParam(name = "limit",defaultValue = "10") int limit, String personName,String deptName,String personPhone,String companyName){
	        String result = industryCategoryService.queryDeptListByPropertity();
	        return result;
	    }
	    
	    /**
	     * 跳转到添加企业类别信息页面
	     * @return
	     */
	    @RequestMapping("industryCategoryEditPage")
	    public String industryCategoryEditPage(String id,Model model){
	    	   if(id!=null){
	    		   TIndustryCategory industry =industryCategoryService.queryRoleById(Integer.parseInt(id));
	               Integer parentId = industry.getT_i_id();//父级部门id
	               TIndustryCategory parentIndustry = industryCategoryService.queryRoleById(parentId);
	               
	               model.addAttribute("industry",industry);
	               model.addAttribute("parentIndustry",parentIndustry);
	           }
	        return "core/industryCategory/industryCategoryEdit";
	    }
	    
	    /**
	     * 保存行业类型信息
	     * @param
	     * @return
	     */
	    @RequestMapping("saveIndustryCategory")
	    public String saveIndustryCategory(HttpServletRequest request){
	    	String id=request.getParameter("id");
	    	String name=request.getParameter("name");
	    	String code=request.getParameter("code");
	    	String t_i_id=request.getParameter("t_i_id");
			if(id!=null&&id!=""){
	            //修改
				TIndustryCategory industry = industryCategoryService.queryRoleById(Integer.valueOf(id));
				industry.setName(name);
				industry.setCode(code);
				if(t_i_id==""){
					industry.setT_i_id(null);
				}else{
				industry.setT_i_id(Integer.valueOf(t_i_id));
				}
	        	industryCategoryService.editAcc(industry);
	        }else{
	            //添加
	        	TIndustryCategory industry=new TIndustryCategory();
	        	industry.setName(name);
				industry.setCode(code);
				industry.setDeleteFlag("0");
				industry.setCreateTime(new Date());
				if(t_i_id==""){
					industry.setT_i_id(null);
				}else{
				industry.setT_i_id(Integer.valueOf(t_i_id));
				}
	        	industryCategoryService.addIndustryCategory(industry);
	        }
	        return "redirect:industryCategoryPage";
	    }
	    
	    /**
	     * 删除行业类别
	     * @param personId
	     * @return
	     */
	    @RequestMapping("/delIndustryCategory")
	    @ResponseBody
	    public Integer delIndustryCategory(String id){
	    	return industryCategoryService.delIndustryCategory(id);
	        
	    }
	    /**
	     * 新增、修改行业Tree页
	     *
	     * @return
	     */
	    @RequestMapping("/industryTree")
	    public String industryTree(Model model) {
	        List<Map<String, Object>> jsonArray = industryCategoryService.queryIndustryNewTree();
	        model.addAttribute("json", JSONArray.toJSON(jsonArray));
	        return "core/industryCategory/industryNewTree";
	    }
}
